package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Users;
import dao.UsersDAO;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public UsersServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String submittype = (String) request.getParameter("submittype");
		switch(submittype) {
		case "登录": getPsw(request,response);break;
		case "注册": insert(request,response);break;
		case "查询指定用户": getUser(request,response);break;
		case "查看所有用户": getAll(request,response);break;
		case "修改当前用户": update(request,response);break;
		case "删除当前用户": delete(request,response);break;
		case "退出登录": quit(request,response);break;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void getPsw(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name = request.getParameter("username");
		String psw = request.getParameter("password");
		boolean u = new UsersDAO().get(name,psw);
		if (u == true) {
			Users user=new UsersDAO().getUser(name, psw);
			request.getSession().setAttribute("UserMessage", user);
			request.getRequestDispatcher("user/control.jsp").forward(request, response);
		}
		else {
			request.setAttribute("error", "用户名或密码错误");
			request.getRequestDispatcher("user/login.jsp").forward(request, response);
		}
	}
	
	private void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name1=request.getParameter("username");
		String name2=request.getParameter("nickname");
		String psw=request.getParameter("password");
		boolean f = new UsersDAO().getUser(name1);
		if(f==false) {
		boolean u=new UsersDAO().insert(name1,name2, psw);
			if (u == true) {
				request.setAttribute("error", "注册成功");
				request.setAttribute("username", name1);
				request.setAttribute("nickname", name2);
				request.setAttribute("password", psw);
				request.getRequestDispatcher("/user/showadd.jsp").forward(request, response);
			}
			else {
				request.setAttribute("error", "注册失败");
				request.getRequestDispatcher("/user/login.jsp").forward(request, response);
			}
		}else {
			request.setAttribute("error", "注册失败");
			request.getRequestDispatcher("/user/add.jsp").forward(request, response);
		}
	}
	
	private void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name=request.getParameter("name");
		List<Users> u=new UsersDAO().get(name);
		if (u !=null) {			
			request.setAttribute("list",u);
			request.getRequestDispatcher("/user/showuser.jsp").forward(request, response);
		}
		else {
			request.setAttribute("error", "查询失败");
			request.getRequestDispatcher("/user/showuser.jsp").forward(request, response);
		}
	}
	
	private void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		List<Users> u=new UsersDAO().getAll();
		if (u !=null) {			
			request.setAttribute("list",u);
			request.getRequestDispatcher("/user/showuser.jsp").forward(request, response);
		}
		else {
			request.setAttribute("error", "查询失败");
			request.getRequestDispatcher("/user/showuser.jsp").forward(request, response);
		}
	}
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		Users obj=(Users)request.getSession().getAttribute("UserMessage");
		String name1 =obj.getUsername();
		String name2 =request.getParameter("nickname");
		String psw =request.getParameter("password");
		boolean u = new UsersDAO().update(name1,name2,psw);
		if (u == true) {
			request.setAttribute("error", "修改成功");
			obj=new UsersDAO().getUser(name1, psw);
			request.getSession().setAttribute("UserMessage", obj);
			request.getRequestDispatcher("/user/showupdate.jsp").forward(request, response);
		}
		else {
			request.setAttribute("error", "修改失败");
			request.getRequestDispatcher("/user/control.jsp").forward(request, response);
		}
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		Users obj=(Users)request.getSession().getAttribute("UserMessage");
		String name=obj.getUsername();
		boolean u=new UsersDAO().delete(name);
		if (u ==true) {			
			request.setAttribute("error","删除成功");
			request.getRequestDispatcher("/user/login.jsp").forward(request, response);
		}
		else {
			request.setAttribute("error", "无法删除");
			request.getRequestDispatcher("/user/control.jsp").forward(request, response);
		}
	}
	
	private void quit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.getSession().removeAttribute("UserMessage");		
		request.setAttribute("error","已退出");
		request.getRequestDispatcher("user/login.jsp").forward(request, response);
	}
}
